package com.itheima.service.impl;

import com.itheima.dao.AddressDao;
import com.itheima.entity.Address;
import com.itheima.entity.PageBean;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Override
    public Address findByAid(int aid) {

        return null;
    }

    @Override
    public PageBean<Address> findAddress(Map<String, Object> condition) {
        int current = (int) condition.get("current");
        int size = (int) condition.get("size");
        int begin = (current - 1) * size;
        condition.put("begin", begin);
        List<Address> data = addressDao.findByUid(condition);
        int count = addressDao.findCount(condition);
        return new PageBean<>(data, count, current, size);
    }

    @Override
    public void save(Address address) {
        address.setIsdefault("0");
        addressDao.save(address);
    }

    @Override
    public void update(Address address) {
        addressDao.update(address);

    }

    @Override
    public void delete(Integer aid) {
        addressDao.delete(aid);

    }

    @Override
    @Transactional
    public void changeDefaultAddress(Integer uid, Integer aid) {
        addressDao.updateAddressIsNotDefault(uid);
        addressDao.updateAddressIsDefault(aid);
    }
}
